/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import com.rits.cloning.Cloner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.cx1.dao.api.IWeberDao;
import org.cx1.dao.impl.WeberDao;
import org.cx1.model.Weber;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IEmailService;
import org.cx1.service.api.IWeberService;
import org.cx1.service.utils.IDGenerator;

/**
 *
 * @author ECH
 */
public class DWeberService implements IWeberService {
    
    private IWeberDao weberDao = new WeberDao();
    private IEmailService emailService = new MEmailService();
    
    private Cloner cloner = new Cloner();
    
    @Override
    public ServiceResponse<String> create(Weber weber) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Weber entry = new Weber();
            id = IDGenerator.generateID();
            
            entry.setListName(weber.getListName());
            entry.setEmail(weber.getEmail());
                        
            weberDao.add(weber);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Weber> read(String id) {
        ServiceResponse<Weber> response = new ServiceResponse<Weber>();
        Weber weber = null;
        try {            
            weber = weberDao.getById(id);
            response.setResponse(weber);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<List<Weber>> readAll(int offset, int limit) {
        ServiceResponse<List<Weber>> response = new ServiceResponse<List<Weber>>();
        List<Weber> webers = new ArrayList<Weber>();
        try {            
            List<Weber> records = weberDao.getAll(offset, limit); 
            response.setResponse(records);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> update(Weber weber) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {      
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> delete(String listName, String email) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            weberDao.deleteById(listName, email);
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
    
    private List<Weber> cloneAmenities(List<Weber> amenities) {
        List<Weber> cloneList = new ArrayList<Weber>(0);
        for (Weber weber : amenities) {
            cloneList.add(cloneWeber(weber));
        }
        return cloneList;
    }
    
    private Weber cloneWeber(Weber weber) {
        return cloner.deepClone(weber);
    }

    @Override
    public ServiceResponse<List<Weber>> readAllByListName(String listName, int offset, int limit) {
        ServiceResponse<List<Weber>> response = new ServiceResponse<List<Weber>>();
        List<Weber> webers = new ArrayList<Weber>();
        try {            
            List<Weber> records = weberDao.getAllByListName(listName, offset, limit);
            response.setResponse(records);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> sendEmailToList(String title, String body, String listName, int offset, int limit) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        try {            
            List<Weber> records = weberDao.getAllByListName(listName, offset, limit);
            if (records != null && records.size() > 0) {
                for (Weber weber : records) {
                    this.emailService.sendEmail(weber.getEmail(), title, body);
                }
            }
            response.setResponse(true);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
        
}
